package com.yoyo.yoyoplat.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.Serializable;

public class SpUtil {

    private static SharedPreferences sp;
    private static final String FILL_NAME = "yp_config";

    public static void init(Context context) {
        sp = context.getSharedPreferences(FILL_NAME, Context.MODE_PRIVATE);
    }

    private void setSp(String key, String value) {
        if (null != sp) {
            sp.edit().putString(key, value).apply();
        }
    }

    private static final String REAL_IP_ADDRESS = "realIpAddress";

    public static void setRealIpAddress(String realIpAddress, int saveTime) {
        SpSaveModel<String> spSaveModel = new SpSaveModel<>(saveTime, realIpAddress, System.currentTimeMillis());
        sp.edit().putString(REAL_IP_ADDRESS, spSaveModel.toString()).apply();
    }

    public static String getRealIpAddress() {
        return SpSaveModel.parseModel(sp.getString(REAL_IP_ADDRESS, ""));
    }

    public static class SpSaveModel<T> implements Serializable {
        private int saveTime;
        private T value;
        private long currentTime;

        public SpSaveModel() {
        }

        public static <T> T parseModel(String s) {
            try {
                SpSaveModel<T> t = new Gson().fromJson(s, new TypeToken<SpSaveModel<T>>() {
                }.getType());
                if (isTimeOut(t.currentTime, t.saveTime)) {
                    return t.value;
                }
            } catch (Exception ignored) {
            }
            return null;
        }

        static <T> T parseModel(String s, T def) {
            try {
                SpSaveModel<T> t = new Gson().fromJson(s, new TypeToken<SpSaveModel<T>>() {
                }.getType());
                if (isTimeOut(t.currentTime, t.saveTime)) {
                    return t.value;
                }
            } catch (Exception ignored) {
            }
            return def;
        }

        private static boolean isTimeOut(long saveCurrentTime, int saveTime) {
            return System.currentTimeMillis() - saveCurrentTime < saveTime * TimeUtils.SECOND;
        }


        SpSaveModel(int saveTime, T value, long currentTime) {
            this.saveTime = saveTime;
            this.value = value;
            this.currentTime = currentTime;
        }

        public long getCurrentTime() {
            return currentTime;
        }

        public void setCurrentTime(long currentTime) {
            this.currentTime = currentTime;
        }

        public int getSaveTime() {
            return saveTime;
        }

        public void setSaveTime(int saveTime) {
            this.saveTime = saveTime;
        }

        public T getValue() {
            return value;
        }

        public void setValue(T value) {
            this.value = value;
        }

        @NonNull
        @Override
        public String toString() {
            return new Gson().toJson(this);
        }
    }
}
